//
//  MyInvestmentTController.m
//  DianDangDai
//
//  Created by bhf on 2017/12/17.
//  Copyright © 2017年 bhf. All rights reserved.
//

#import "MyInvestmentTController.h"
#import "InvestmentBillCell.h"
#import "MyInvestModel.h"
#import "MyInvestDetailController.h"
@interface MyInvestmentTController ()
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) NSInteger type;
@end

@implementation MyInvestmentTController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的出借账单";
    _type = 1;
    [self requestDataWithorderType:_type];
    self.navigationItem.backBarButtonItem = BarButtonItem;
}

- (void)requestDataWithorderType:(NSInteger)type {
    self.dataArray = [NSMutableArray array];
    NSString *urlString = [NSString stringWithFormat:@"%@OPT=1023&condition=%ld&currPage=1&sign=%@",API_LOGIN_URL_STRING,type,USER_SIGN];
    [[CDHttpHelper defaultCDHttpHelper] get:urlString params:nil success:^(id responseObj) {
        NSDictionary *dict = [responseObj objectFromJSONData];
        NSLog(@"%@",dict);
        NSInteger code = [dict[@"code"] integerValue];
        if (code == -1) {
            [self.view makeToast:dict[@"msg"]];
        }
        for (NSDictionary *dic in dict[@"list"]) {
            MyInvestModel *model = [MyInvestModel mj_objectWithKeyValues:dic];
            [self.dataArray addObject:model];
        }
        //[self.tableView.mj_header endRefreshing];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    } showHUD:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    InvestmentBillCell *cell = [tableView dequeueReusableCellWithIdentifier:@"investment_cell" forIndexPath:indexPath];
    MyInvestModel *model = self.dataArray[indexPath.row];
    [cell setModel:model];
    if (_type == 1) {
        [cell setLeftLabelColor:THE_MASS_TONE_ATTUNE];
    } else if (_type == 2) {
        [cell setLeftLabelColor:[UIColor colorWithHex:@"#73e379"]];
    }
    // Configure the cell...
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}
- (IBAction)changeData:(UIButton *)sender {
    
    if (sender.tag == 101) {
        
        if (_type == 1) {
            return;
        } else {
            _type = 1;
            [_btn3 setTitleColor:[UIColor colorWithHex:@"#333333"] forState:UIControlStateNormal];
            [_btn2 setTitleColor:[UIColor colorWithHex:@"#333333"] forState:UIControlStateNormal];
            [sender setTitleColor:THE_MASS_TONE_ATTUNE forState:UIControlStateNormal];
            [self requestDataWithorderType:_type];
        }
        
        return;
    }
    if (sender.tag == 102) {
        if (_type == 2) {
            return;
        } else {
            _type = 2;
            [_btn1 setTitleColor:[UIColor colorWithHex:@"#333333"] forState:UIControlStateNormal];
            [_btn3 setTitleColor:[UIColor colorWithHex:@"#333333"] forState:UIControlStateNormal];
            [sender setTitleColor:THE_MASS_TONE_ATTUNE forState:UIControlStateNormal];
            [self requestDataWithorderType:_type];
        }
        return;
    }
    if (sender.tag == 103) {
        if (_type == 3) {
            return;
        } else {
            [_btn1 setTitleColor:[UIColor colorWithHex:@"#333333"] forState:UIControlStateNormal];
            [_btn2 setTitleColor:[UIColor colorWithHex:@"#333333"] forState:UIControlStateNormal];
            _type = 3;
            [sender setTitleColor:THE_MASS_TONE_ATTUNE forState:UIControlStateNormal];
            [self requestDataWithorderType:_type];
        }
        return;
    }
    
}
- (IBAction)checkDetail:(UIButton *)sender {
    InvestmentBillCell *cell = (InvestmentBillCell *)[[sender superview] superview];
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    MyInvestModel *model = self.dataArray[indexPath.row];
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"My" bundle:[NSBundle mainBundle]];
    MyInvestDetailController *myInvest = [sb instantiateViewControllerWithIdentifier:@"mid"];
    
    myInvest.type = _type;
    myInvest.investSign = model.investSign;
    [self showViewController:myInvest sender:self];
    
}



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
